package jee.boot.module.system.rest;

import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import jee.boot.common.basic.BaseController;
import jee.boot.common.basic.Page;
import jee.boot.common.basic.result.ResponseMessage;
import jee.boot.common.basic.result.Result;
import jee.boot.entity.auth.Account;
import jee.boot.entity.system.Filter;
import jee.boot.common.properties.ProjectConfig;
import jee.boot.common.standard.AccountHandler;
import jee.boot.common.service.AreaService;
import jee.boot.common.service.DictionaryService;
import jee.boot.module.authority.service.ResourceService;
import jee.boot.module.authority.service.UserService;
import jee.boot.entity.auth.Resource;
import jee.boot.entity.auth.User;
import jee.boot.entity.system.Area;
import jee.boot.entity.system.Dictionary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 初始化数据接口
 *
 * @author zhilei.wang
 */
@Api(tags = "系统数据初始化")
@RestController
@RequestMapping
public class InitRestController extends BaseController {
    @Autowired
    private AccountHandler accountHandler;
    @Autowired
    private ProjectConfig projectConfig;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private AreaService areaService;
    @Autowired
    private UserService userService;

    /**
     * 获取初始化数据
     *
     * @return
     */
    @PostMapping("/initData")
    public ResponseMessage<Map<String, Object>> initData() {
        Map<String, Object> result = new HashMap<>();
        Account account=accountHandler.getPrincipal();
        User authorization = userService.authorization(new User(account));
        if (projectConfig.getInitData().contains("user")) {
            // 初始化用户
            result.put("user", authorization);
            // 初始化账户
            result.put("account", account);
        }
        if (projectConfig.getInitData().contains("menu")) {
            List<Resource> menuList= Lists.newArrayList();
            if(accountHandler.isSuperAdministrator()){
                menuList.addAll(resourceService.findList(new Resource()));
            }else{
                menuList.addAll(authorization.getResourceSet());
            }
            result.put("menu", resourceService.buildTree(menuList));
        }
        if (projectConfig.getInitData().contains("dict")) {
            // 初始化字典
            Dictionary dictionary = new Dictionary();
            dictionary.setPage(new Page<>());
            dictionary.getPage().setOrderBy("sort");
            dictionary.getPage().setOrderSort("asc");
            result.put("dict",dictionaryService.findList(dictionary));
        }
        if (projectConfig.getInitData().contains("area")) {
            // 初始化行政区划
            Area area = new Area();
            area.setPage(new Page<>());
            area.getPage().setOrderBy("code");
            area.getPage().setOrderSort("asc");
            area.addFilter(new Filter("type", "in", new ArrayList<String>() {{
                add("2");
                add("3");
            }}));
            result.put("area", areaService.buildTree(areaService.findList(area)));
        }
        return Result.success(result);
    }


}
